package impl.cglibimpl;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * Created by chenjun on 2017/11/22.
 */
public class CglibInterceptor implements MethodInterceptor {
    final Object delegate;

    CglibInterceptor(Object delegate) {
        this.delegate = delegate;
    }

    /**
     *
     * @param object 生成的代理类
     * @param method 被代理的方法（当前被调用）
     * @param objects 传递给方法的参数
     * @param methodProxy cglib对被代理方法的一个包装
     * @return
     * @throws Throwable
     */
    @Override
    public Object intercept(Object object, Method method, Object[] objects,
                            MethodProxy methodProxy) throws Throwable {
        before();
        Object result = methodProxy.invoke(delegate, objects);
        after();
        return  result;
    }

    public  void before(){
        System.out.println("---cglib before----");

    }

    public  void after(){
        System.out.println("---cglib after----");
    }
}
